<?php

class ModuleLayoult extends fvLayoult {

	private $_pageInstance = null;
	private $_domInstance = null;

	function __construct(){
		$currentUrl = fvRoute::getInstance()->getModuleName();

		$p = PageManager::getInstance()->getByPageUrl($currentUrl);
		if (is_object($p[0])) {
			$this->_pageInstance = $p[0];
		} elseif (is_object($p = PageManager::getInstance()->getOneByPageName('default'))) {
			$this->_pageInstance = $p;
		} else fvDispatcher::redirect(fvSite::$fvConfig->get('page_404', 0, 404));

		$this->_domInstance = new DOMDocument();

		if (!($this->_domInstance->loadXML($this->_pageInstance->getPageContent()))) {
			fvDispatcher::redirect(fvSite::$fvConfig->get('page_error', 0, 302));
		}

		$this->setTitle($this->_pageInstance->page_title);
		$this->setDescription($this->_pageInstance->page_description);
		$this->setKeywords($this->_pageInstance->page_keywords);

		parent::__construct("main.tpl");
	}

	function getPageContent() {

	}

	function getPageContentPart($contentPartName) {
		$xpth = new DOMXPath($this->_domInstance);
		$currentPart = $xpth->evaluate("/page/content_part[@name='$contentPartName']");
		if ($currentPart->length == 1) {
			$currentPart = $currentPart->item(0);
		}
		else{
			return '';
		}
		return $this->_parseNode($currentPart);
	}

	private function _parseNode($node) {
		$result = '';
		$childNodes = array();
		foreach ($node->childNodes as $childNode) {
			$childNodes[] = $childNode;
		}
		usort($childNodes, array($this, '__cmp'));

		switch ($node->nodeName) {
			case 'module':
				$moduleName = $node->getAttribute('name');
				$moduleView = $node->getAttribute('view');
				$moduleParams = unserialize($node->getAttribute('parameters'));
				$module = fvDispatcher::getInstance()->getModule($moduleName, "module");
				return $module->showModule($moduleView, $moduleParams, $node->getAttribute("id"));
			case 'current_module':
				return $this->getModuleResult();
				break;
			case 'vertical_layoult':
				$data = '';
				$data .= "<table style='width: 100%' cellspacing=0 cellpadding=0 valign=top>";
				$heights = explode(',', $node->getAttribute('size'));
				$spacer = $node->getAttribute('spacer');
				foreach ($childNodes as $key => $childNode) {
					$data .= "<tr" . (($heights[$key] != "*")?" height='{$heights[$key]}'":'') ."><td valign='top'>".$this->_parseNode($childNode)."</td></tr>";
					if ($spacer) {
						$data .= "<tr height='{$spacer}px'><td>&nbsp;</td></tr>";
					}
				}
				$data .= "</table>";
				return $data;
			case 'horisontal_layoult':
				$data = '';
				$data .= "<table style='width: 100%' cellspacing=0 cellpadding=0 valign=top><tr>";
				$widths = explode(',', $node->getAttribute('size'));
				$spacer = $node->getAttribute('spacer');
				foreach ($childNodes as $key => $childNode) {
					$data .= "<td valign='top' height='100%'" . (($widths[$key] != "*")?" width='{$widths[$key]}'":'') .">".$this->_parseNode($childNode)."</td>";
					if ($spacer && ($key < (count($childNodes) - 1))) {
						$data .= "<td width='{$spacer}px'>&nbsp;</td>";
					}
				}
				$data .= "</tr></table>";
				return $data;
			case 'content_part':
				foreach ($childNodes as $childNode) {
					$result .= $this->_parseNode($childNode);
				}
				break;
		}
		return $result;
	}

	private function __cmp($a, $b) {

		if ($a->getAttribute('order') == $b->getAttribute('order')) return 0;

		return ($a->getAttribute('order') < $b->getAttribute('order')) ? -1 : 1;
	}

}

?>
